Writing to the Database
You have now built the form and users can supply data for the new bicycle part. You must now write the code to:
- Send the bicycle part information to the database
- Display a part ID number that returns after the database insert.
Sending the data to the database is fairly straightforward. You simply call the method and pass the arguments the method expects. In this case, you will be passing the bicycle part information.
Since the method returns a part ID, you will need to write a responder function just as you have done with the two previous database calls.
Exercise 5
In this exercise, you will examine the database interactions, create methods to write data to the database, set a click handler to call a method, display the new ID for a new record, and update the ListBox.
-
Examining the Database Interaction Code
- Open the CFC in Dreamweaver MX.
- For this exercise you will use the most
complicated function in the CFC—the insertPart function. You can separate the functionality of this function into several categories:
- The first five lines of code declare
the function and require that the name, description, price,
and part type information be passed to the function as arguments,
as shown here:
<cffunction name="insertPart" access="remote" returntype="numeric"> <cfargument name="name" required="true" type="string"> <cfargument name="description" required="true" type="string"> <cfargument name="price" required="true" type="numeric"> <cfargument name="partTypeID" required="true" type="numeric">
- The next section of code uses a
cftransactiontag. You use this tag to ensure that the twocfquerytags do not execute simultaneously. The user's interaction can execute the firstcfquerybefore the second, independently. This ensures that the new data inserts before the app tries to read the new part ID read (for the data) before another user can insert data. This is necessary so two different users do not receive the same part ID. You can read more about transaction-based queries in Simon Horwith's ColdFusion Transaction Basics article, soon to be published. - After the
cftransactiontag is the first query, which inserts the data and is the first part of the transaction, as follows:<cfquery datasource="bikeparts"> INSERT INTO Parts (name,description,price,partTypeID) VALUES ('#name#','#description#',#price#,#partTypeID#) < /cfquery> -
When the new data is written, the database creates a unique identifier for that new row of data. This is called a primary key. The next query reads the primary key, or ID, by retrieving the largest value from the primary key column of the database, which is specifically called partID. The query is as follows:
<cfquery name="qPartID" datasource="bikeparts"> SELECT max(partID) AS partNumber FROM Parts < /cfquery>
-
The last three lines of code end the transaction, return the new partID to Macromedia Flash, and close the function, as follows:
< /cftransaction> <cfreturn qPartID.partNumber> </cffunction>
- The first five lines of code declare
the function and require that the name, description, price,
and part type information be passed to the function as arguments,
as shown here:
-
Creating the Method to Send Data to the Database for Insertion
-
In devnet.fla, in the object creation section, create an object named insertResponder using the following syntax:
var insertResponder=new Object();
-
At the top of the object methods section, create the skeleton for a method named insertData, which is attached to the addPart_pb object, as follows:
addPart_pb.insertData=function(){ } -
In the insertData method, call the partService.insertPart() method. Pass the following arguments, in the order shown:
- insertResponder
- name_txt.text
- desc_txt.text
- Number(price_txt.text)
- Number(types_cb.getSelectedItem().data)
Note: Remember that Macromedia Flash Remoting will automatically respond the first argument. Use the
Number()function because data from TextBoxes is always of type String, and the database expects numbers for the price and part type. -
Ensure your method appears as follows:
addPart_pb.insertData=function(){ partService.insertPart(insertResponder, name_txt.text, desc_txt.text, Number(price_txt.text), Number(types_cb.getSelectedItem().data)); }
-
-
Set the Click handler for the PushButton to call the new method. In the event handler assignment section, use the
setClickHandler()method to haveaddPart_pb invoketheinsertDatamethod when clicked, as follows:addPart_pb.setClickHandler("insertData",addPart_pb); -
Display the returned Part ID in the onResult method.
-
At the top of the object methods section, create the skeleton for a method named insertResponder.onResult method. Use the variable itemNumber for the argument, as follows:
insertResponder.onResult=function(itemNumber){ } - In the method, ensure newItem_txt is visible.
- In the method, use newItem_txt to display the new part ID.
-
Ensure your method appears as follows:
insertResponder.onResult=function(itemNumber){ newItem_txt._visible=true; newItem_txt.text="The ID for your new product is "+itemNumber; } -
When the user clicks to continue, you want the newItem_txt to disappear. Add the following as the first line of code in the parts_lb.displayOrAdd() method:
newItem_txt._visible=false;
- Save the document and test the movie. You should now be able to display all the part information and add new part information as well.
-
-
Updating the ListBox Immediately After Adding a New Record
- Rather than reporting a part ID, you might wish to have the new record appear in the ListBox as soon as it has been inserted into the database. To start this process, remove or comment the two lines of code concerning newItem_txt in the insertResponder.onResult method.
- In the insertResponder.onResult method call the partService.getPartInfo() method.
-
Check to be sure your method appears as follows:
insertResponder.onResult=function(itemNumber){ partService.getPartInfo(partResponder); } - Save the document and test the movie. Insert a new part and it will appear immediately in the ListBox.
Submit feedback on our tutorials, articles, and sample applications.